package com.cnbot.dchttpsdk.rx;

import android.content.Context;
import android.os.Looper;

import com.cnbot.dchttpsdk.helper.OnSubscriberListener;
import com.cnbot.dchttpsdk.helper.ProgressCancelListener;
import com.cnbot.dchttpsdk.view.IProgressView;
import com.cnbot.dchttpsdk.view.ProgressView;
import com.cnbot.dchttpsdk.view.ProgressViewHandler;

/**
 * Copyright (c) 2016-a  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin
 * @FileName: ProgressViewSubscriber.java
 * @author: dc
 * @date: 2019/1/19 14:13
 * @version: 1.0
 */

public class ProgressViewSubscriber extends DefaultSubscriber implements ProgressCancelListener {
    private final String TAG = "ProgressViewSubscriber";
    private ProgressViewHandler mProgressHandler;

    /**
     * 默认使用系统的请求加载弹窗
     * @param nextListener
     * @param context
     */
    public ProgressViewSubscriber(OnSubscriberListener nextListener, Context context) {
        super(nextListener, context);
        if (!inMainThread()) {
            mProgressHandler = new ProgressViewHandler(Looper.getMainLooper(), context);
            new ProgressView(context, mProgressHandler,this);
        } else{
            ProgressView progressView = new ProgressView(context, this);
            mProgressHandler = new ProgressViewHandler(context, progressView);

        }
    }

    public ProgressViewSubscriber(OnSubscriberListener nextListener, Context context, IProgressView progressView) {
        super(nextListener, context);
        if (!inMainThread()) {
            mProgressHandler = new ProgressViewHandler(Looper.getMainLooper(), context, progressView);
        } else {
            mProgressHandler = new ProgressViewHandler(context, progressView);
        }
    }

    private void showProgressDialog() {
        if (mProgressHandler != null) {
            mProgressHandler.obtainMessage(ProgressViewHandler.SHOW_PROGRESS_VIEW).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressHandler != null) {
            mProgressHandler.obtainMessage(ProgressViewHandler.DISMISS_PROGRESS_VIEW).sendToTarget();
            mProgressHandler = null;
        }
    }

    private void dismissProgressDialog(long delay) {
        if (mProgressHandler != null) {
            mProgressHandler.sendEmptyMessageDelayed(ProgressViewHandler.DISMISS_PROGRESS_VIEW_DELAY, delay);
            mProgressHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        super.onStart();
        showProgressDialog();
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        super.onCompleted();
        dismissProgressDialog();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        super.onError(e);
        dismissProgressDialog();
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}